package com.wangshaoyu.music_player.repository;

import com.wangshaoyu.music_player.data_object.TrackDo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.io.InputStream;
import java.util.List;

@Mapper
@Repository
public interface TrackRepo {
    @Insert("insert into tracks (uid, title, type, content) values (#{uid}, #{title}, #{type}, #{content})")
    void insert1(
            @Param("uid") int uid,
            @Param("title") String title,
            @Param("type") String type,
            @Param("content") InputStream content);

    @Insert("insert into tracks (uid, title, type, content) values (#{uid}, #{title}, #{type}, #{content})")
    void insert2(
            @Param("uid") int uid,
            @Param("title") String title,
            @Param("type") String type,
            @Param("content") byte[] content);

    @Select("select type, content from tracks where tid = #{tid}")
    TrackDo selectOneByTid(@Param("tid") int tid);

    @Select("select tid, title from tracks where uid = ${uid} order by tid desc")
    List<TrackDo> selectListByUid(@Param("uid") int uid);


    @Select({
            "<script>",
            "select tid, title from tracks where tid in (",
            "<foreach collection='tidList' item='id' separator=', '>",
                "#{id}",
            "</foreach>",
            ")",
            "</script>"})
    List<TrackDo> selectListByTidList(@Param("tidList") List<Integer> tidList);

    @Select({
            "<script>",
            "select tid, title from tracks where tid not in (",
            "<foreach collection='tidList' item='id' separator=', '>",
            "#{id}",
            "</foreach>",
            ") and uid = #{uid} order by tid desc",
            "</script>"})
    List<TrackDo> selectListByUidAndNotInTidList(
            @Param("uid") int uid,
            @Param("tidList") List<Integer> tidList);
}
